package com.nuoyun.pro;

import com.alibaba.druid.translation.TranslationConfig;
import com.alibaba.druid.translation.TranslationFilter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AutoConfiguration {

    @Bean
    @ConfigurationProperties(FILTER_TRANSLATION_CONFIG_PREFIX)
    @ConditionalOnProperty(prefix = FILTER_TRANSLATION_PREFIX, name = "enabled")
    @ConditionalOnMissingBean
    public TranslationConfig translationConfig() {
        return new TranslationConfig();
    }

    @Bean
    @ConfigurationProperties(FILTER_TRANSLATION_PREFIX)
    @ConditionalOnProperty(prefix = FILTER_TRANSLATION_PREFIX, name = "enabled")
    @ConditionalOnMissingBean
    public TranslationFilter translationFilter(TranslationConfig translationConfig) {
        TranslationFilter filter = new TranslationFilter();
        filter.setConfig(translationConfig);
        return filter;
    }

    private static final String FILTER_TRANSLATION_PREFIX = "spring.datasource.druid.filter.translation";
    private static final String FILTER_TRANSLATION_CONFIG_PREFIX = FILTER_TRANSLATION_PREFIX + ".config";
}
